package com.telchina.workorder.controller;

import com.telchina.common.annotation.Log;
import com.telchina.common.core.controller.BaseController;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.enums.BusinessType;
import com.telchina.common.utils.DateUtils;
import com.telchina.common.utils.SecurityUtils;
import com.telchina.common.utils.poi.ExcelUtil;
import com.telchina.workorder.domain.WoExtension;
import com.telchina.workorder.domain.vo.WoExtensionVO;
import com.telchina.workorder.service.IWoExtensionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 延期申请Controller
 *
 * @author konghao
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/workorder/woExtension")
public class WoExtensionController extends BaseController {
    @Autowired
    private IWoExtensionService woExtensionService;


    /**
     * 分页查询延期申请列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:extension:list')")
    @GetMapping("/list")
    public AjaxResult list(WoExtension woExtension) {
        startPage();
//        IPage<WoExtension> list = woExtensionService.selectList(startPage(), woExtension);
        List<WoExtensionVO> list = woExtensionService.selectListAll(woExtension);
        return getDataTable(list);
    }

    /**
     * 查询延期申请列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:extension:list')")
    @GetMapping("/list/all")
    public AjaxResult listAll(WoExtension woExtension) {
        List<WoExtensionVO> listVO = woExtensionService.selectListAllAndAttachment(woExtension);
        return AjaxResult.success(listVO);
    }

    /**
     * 导出延期申请列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:extension:export')")
    @Log(title = "延期申请" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WoExtension woExtension) {
        List<WoExtensionVO> list = woExtensionService.selectListAll(woExtension);
        ExcelUtil<WoExtensionVO> util = new ExcelUtil<WoExtensionVO>(WoExtensionVO.class);
        return util.exportExcel(list, "extension");
    }

    /**
     * 获取延期申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:extension:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(woExtensionService.selectById(id));
    }

    /**
     * 新增延期申请
     */
//    @PreAuthorize("@ss.hasPermi('workorder:extension:add')")
    @Log(title = "延期申请" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WoExtension woExtension) throws Exception {
        return woExtensionService.insert(woExtension);
    }

    /**
     * 修改延期申请
     */
//    @PreAuthorize("@ss.hasPermi('workorder:extension:edit')")
    @Log(title = "延期申请" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WoExtension woExtension) {
        return toAjax(woExtensionService.update(woExtension));
    }

    /**
     * 删除延期申请
     */
//    @PreAuthorize("@ss.hasPermi('workorder:extension:remove')")
    @Log(title = "延期申请" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(woExtensionService.deleteByIds(ids));
    }

    /**
     * 获取延期申请详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:extension:add')")
    @GetMapping(value = "/getInitExtension")
    public AjaxResult getInitExtension() {
        Map<String, Object> result = new HashMap<>();
        // 获取登录人所在部门信息
        result.put("filledTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
        result.put("extensionNo", DateUtils.parseDateToStr("yyMMdd", new Date()));
        result.put("filledBy", SecurityUtils.getLoginUser().getUsername());
        return AjaxResult.success(result);
    }

    /**
     * 工作流程相关接口开始
     */
    /**
     * 分页查询工单信息我的代办列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:list')")
    @GetMapping("todoList")
    public AjaxResult todoList(WoExtension extension) {
        List<WoExtensionVO> list = woExtensionService.selectTodoList(extension);
        return getDataTable(list);
    }

    /**
     * 审核处理
     */
//    @PreAuthorize("@ss.hasPermi('workorder:order:add')")
    @Log(title = "审核处理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/approve")
    public AjaxResult approveExtension(@RequestBody Map<String, Object> maps) throws Exception {
        return woExtensionService.approveExtension(maps);
    }
}
